var TYPE_ADD=1;
var TYPE_UPDATE=2;
var TYPE_REMOVE=3;
var TYPE_MOVE=4;
var TYPE_MOVEBACK=5;
var TYPE_CLEAR=6;
var TYPE_UPDATE_POSITION=7;
var TYPE_REPLACE=8;
var TYPE_RETRIEVE_SLIDES=9;
var TYPE_RETRIEVE=10;
var TYPE_RETRIEVE_ALL=10;	/** same meaning with TYPE_RETRIEVE */

var TYPE_IMAGE_SLIDE='image';
var TYPE_U2B_SLIDE='u2b';
var TYPE_ARTICLE_SLIDE='article';
var TYPE_FB_SLIDE='fb';

var type_array=[TYPE_IMAGE_SLIDE,TYPE_U2B_SLIDE,
              TYPE_ARTICLE_SLIDE,TYPE_FB_SLIDE];

var current_type=null;

var splitter='<nsoft_splitter>';
var server_splitter='<nsoft_server_splitter>';
var inline_splitter='<nsoft_inline_splitter>';
var data_splitter='<nsoft_data_splitter>';
var prime='<nsoft_prime>';
var dprime='<nsoft_dprime>';

var undefined_obj='undefined';

var slides;
var ARRAY_SLIDES='slides';
var ARRAY_IMAGE_SLIDES='imgslides';
var ARRAY_U2B_SLIDES='u2bslides';
var ARRAY_ARTICLE_SLIDES='articleslides';
var ARRAY_FB_SLIDES='fbslides';
var ARRAY_AUDIO_SLIDE='audioslide';
var slide_array_type=[ARRAY_SLIDES,
                      ARRAY_IMAGE_SLIDES,
                      ARRAY_U2B_SLIDES,
                      ARRAY_ARTICLE_SLIDES,
                      ARRAY_FB_SLIDES];

var edit_div='nsoft_editor_div';
var masker='nsoft_masker';
var slide_info_field='nsoft_slide_info_field_';

var slide_idx=0;

function init_array(){
	slides={};
	slides[ARRAY_SLIDES]={};
	slides[ARRAY_IMAGE_SLIDES]={};
	slides[ARRAY_U2B_SLIDES]={};
	slides[ARRAY_ARTICLE_SLIDES]={};
	slides[ARRAY_FB_SLIDES]={};
}

var max_slides_on_line=7;
var width_img=100;
var slide_width=110;
var additional_part=15;
var TIMELINE_WIDTH=970;	/** but it is varied 
						 * due to the actual timeline width */

var PANEL_WIDTH=430; 
var PANEL_HEIGHT=320;

var PROPERTIES_DRAG_INCLUDE=1;
var PROPERTIES_DRAG_EXCLUDE=0;

function init_param(){
	Object.size=function(obj){
		var size=0,key=null;
		for (key in obj){
			if (obj.hasOwnProperty(key)) size++;
		}
		return size;
	};
	
	/** add clear link upon timeline */
	$('#'+a_timeline_clear).click(function(){
		remove_all_timeline();
	});
	
	TIMELINE_WIDTH=$('#'+div_timeline).width();
	
	init_array();
}

function select_array(type){
	switch (type){
		case TYPE_IMAGE_SLIDE:{
			return ARRAY_IMAGE_SLIDES;
		}
		case TYPE_U2B_SLIDE:{
			return ARRAY_U2B_SLIDES;
		}
		case TYPE_ARTICLE_SLIDE:{
			return ARRAY_ARTICLE_SLIDES;
		}
		case TYPE_FB_SLIDE:{
			return ARRAY_FB_SLIDES;
		}
		default:{
			return ARRAY_SLIDES;
		}
	}
}

function open_slide_editor(id,data){
	var e=data.split(splitter);
	var type=e[0];
	var thumb=e[1],img=e[2],
		xdata=e[3].split(data_splitter),
		timeslot=e[4];
	
	$('body').append($('<div id="'+edit_div+'"></div>').css({
			'width':'500px',
			'height':'450px',
		})
	);
	
	$('#'+edit_div).css({
		'top':20,
		'left':($(window).width()-$('#'+edit_div).width())/2,
		'position':'absolute',
		'background':'#fff',
		'opacity':1,
		'z-index':5001,
	});
	
	$('#'+edit_div).append(''+
	'<div>'+
	'	<table><tr><td align="center" colspan="2" style="height:30px;vertical-align:middle;"><b>EDIT SLIDE</b></td><tr>'+
	'	<tr><td style="width:240px;background:#eee;">'+
	'		<div style="position:absolute;left:10px;width:230px;">'+
	'		<p><b>category: '+type+'</b></p>'+
	'		<p><b>thumb:</b> '+thumb+'</p>'+
	'		<p><b>background:</b> '+img+'</p>'+
	'		<p><b>timeslot:</b><br>hh : mm : ss<br>'+
	'			<input id="'+slide_info_field+'1" type="text" value="'+get_time('hh',timeslot)+'" style="width:20px;"> :'+
	'			<input id="'+slide_info_field+'2" type="text" value="'+get_time('mm',timeslot)+'" style="width:20px;"> :'+
	'			<input id="'+slide_info_field+'3" type="text" value="'+get_time('ss',timeslot)+'" style="width:20px;">'+
	'		</p>'+
	'		</div>'+
	'	</td><td style="width:260px;background:#eee;">'+
	'		<img style="width:250px;height:200px;left:0px;top:10px;" src="'+img+'"><br>'+
	'		<span>'+
	'			resolution: '+xdata[0]+'x'+xdata[1]+
	'			<br>'+
	'			size: '+get_size_informat(xdata[2])+
	'			<br>&nbsp;'+
	'		</span>'+
	'	</td></tr><tr><td colspan="2">'+
	'		'+add_sub_editor(type,xdata[3])+
	'	</td></tr>'+
	'	<tr><td colspan="2" align="right" height="100%" style="background:#eee;">'+
	'		<div style="margin-top:10px;margin-right:10px;height:50px;">'+
	'		<input type="button" value="save" style="cursor:pointer;" onclick="save_slide(\''+type+'\',\''+id+'\',\''+data+'\');">&nbsp;'+
	'		<input type="button" value="cancel" style="cursor:pointer;" onclick="close_mask();">'+
	'		</div>'+
	'	</td></tr>'+
	'	</table>'+
	'</div>'+
	'');
	
	add_n_config_mask(type,data);
}

function add_sub_editor(type,xdata){
	switch(type){
		case TYPE_IMAGE_SLIDE:{
			return add_image_editor(xdata);
		}
		case TYPE_U2B_SLIDE:{
			return add_u2b_editor(xdata);
		}
		case TYPE_ARTICLE_SLIDE:{
			return add_article_editor(xdata);
		}
	}
}

function add_image_editor(xdata){
	var desc=xdata!=undefined?xdata:'';
	return ''+
	'	<div style="margin-left:10px;margin-top:3px;margin-bottom:3px;">'+
	'		<b>description</b><br>'+
	'		<textarea id="'+slide_info_field+'4" rows="8" cols="65">'+desc+'</textarea>'+
	'	</div>'+
	'';
}

function add_u2b_editor(xdata){
	var info=xdata.split(inline_splitter);
	return ''+
	'	<div style="margin-left:10px;margin-top:3px;margin-bottom:3px;margin-right:10px;">'+
	'		<b>title</b><br>'+info[0]+'<br><br>'+
	'		<b>description</b><br>'+info[1]+'<br><br>'+
	'		<b>author</b> '+info[3]+'<br><br>'+
	'		<a onclick="window.open(\''+info[4]+'\');">link ></a><br>'+
	'	</div>'+
	'';	
}

function add_article_editor(xdata){
	var info=xdata.split(inline_splitter);
	return ''+
	'	<div style="margin-left:10px;margin-top:3px;margin-bottom:3px;margin-right:10px;">'+
	'		<b>title</b><br>'+info[0]+'<br><br>'+
	'		<b>description</b><br>'+info[2]+'<br><br>'+
	'		<a onclick="window.open(\''+info[3]+'\');">link ></a><br>'+
	'	</div>'+
	'';	
}

function add_n_config_mask(stype,sdata){
	
	$('body').append(''+
	'<div id="'+masker+'" style="top:0;height:0;"></div>');
	$('#'+masker).css({
		'top':0,
		'left':0,
		'width':$(document).width(),
		'height':$(document).height(),
		'position':'absolute',
		'opacity':0.5,
		'background':'#000000',
		'z-index':5000,
	});

	$(window).resize(function(){
		$('#'+masker).width($(document).width());
		$('#'+masker).height($(document).height());
		
		$('#'+edit_div).css({
			'top':0,
			'left':($(window).width()-$('#'+edit_div).width())/2,
		});
		
	});
	
	$('#'+masker).click(function(){
		close_mask();
	});
}

/**
 * sub functions
 */

function save_slide(type,div_id,data){
	
	var e=data.split(splitter);
	var f=e[3].split(data_splitter);
	var sld_info=e[0]+splitter+
				e[1]+splitter+
				e[2]+splitter;
	var sub_sld_inf=f[0]+data_splitter+
					f[1]+data_splitter+
					f[2]+data_splitter;

	var hh=parseInt($('#'+slide_info_field+'1').val());
	var mm=parseInt($('#'+slide_info_field+'2').val());
	var ss=parseInt($('#'+slide_info_field+'3').val());
	e[4]=hh*3600+mm*60+ss;
	
	
	var slide_type='';
	switch(type){
		case TYPE_IMAGE_SLIDE:{
			slide_type=ARRAY_IMAGE_SLIDES;
			var desc=$('#'+slide_info_field+'4').val();
			sub_sld_inf+=desc;
			
			break;
		}
		case TYPE_U2B_SLIDE:{
			slide_type=ARRAY_U2B_SLIDES;
			sub_sld_inf+=f[3];
			break;
		}
		case TYPE_ARTICLE_SLIDE:{
			slide_type=ARRAY_ARTICLE_SLIDES;
			sub_sld_inf+=f[3];
			break;
		}
	}
	sld_info+=sub_sld_inf+splitter;
	for (var i=4;i<e.length;i++){
		sld_info+=e[i]+(i<e.length-1?splitter:'');
	}
	
	$('#a_'+div_id).attr('onclick',
			'open_slide_editor(\''+div_id+'\',\''+sld_info+'\')');
	$('#'+div_id).find('input').val(sld_info);
	
	var content=$('#'+div_id).parent().html();
	content=content.replace(/&lt;/g,'<');
	content=content.replace(/&gt;/g,'>');
	slides[slide_type][div_id]=content;
	contact_session(TYPE_UPDATE,slide_type,div_id,content);
	
	close_mask();
}

function close_mask(){
	$('#'+edit_div).remove();
	$('#'+masker).remove();
}

function get_time(type,timeslot){
	var hh=Math.floor(timeslot/3600);
	var hhStr=hh<10?'0'+hh:hh;
	var rem=timeslot%3600;
	var mm=Math.floor(rem/60);
	var mmStr=mm<10?'0'+mm:mm;
	rem=rem%60;
	var ss=Math.floor(rem);
	var ssStr=ss<10?'0'+ss:ss;
	
	switch(type){
		case 'hh':{
			return hhStr;
		} 
		case 'mm':{
			return mmStr;
		}
		case 'ss':{
			return ssStr;
		}
		case '':{
			return hhStr+':'+mmStr+':'+ssStr;
		}
	}
	return undefined;
}

/**
 * get size in general format with surfix MB and KB
 */
function get_size_informat(size){
	if (size>1024*1024){
		return Math.floor(size/(1024*1024))+'MB';
	}else if (size>1024){
		return Math.floor(size/1024)+'KB';
	}
}